﻿using UnityEngine;
using System.Collections;

// aplaczek 25.11.2014
public class Shooter : MonoBehaviour
{
    // parameters
    public GameObject inBulletPrefab;
    public Transform inBulletSpawnPosition;
    public float minBulletForce = 200.0f;
    public float maxBulletForce = 600.0f;
    public float bulletForceStep = 10.0f;
    public float upAngle = 0.2f;

    private float currentBulletForce;

    private Vector3 currentAngle;
    private Vector3 right;
    private Vector3 left;

    private Vector3 rightTurn;
    private Vector3 leftTurn;

    void Start()
    {
        
        right = new Vector3(1.0f, upAngle, 0.0f);
        left = new Vector3(-1.0f, upAngle, 0.0f);
        currentAngle = right;

        rightTurn = new Vector3(0.0f, 0.0f, 0.0f);
        leftTurn = new Vector3(0.0f, 180.0f, 0.0f);

        currentBulletForce = minBulletForce;

    }


    // Update is called once per frame
    void Update()
    {
        
        // rotate left
        if (Input.GetKey(KeyCode.LeftArrow) == true)
        {
            currentAngle = left;
            transform.localEulerAngles = leftTurn;
        }

        // rotate right
        if (Input.GetKey(KeyCode.RightArrow) == true)
        {
            currentAngle = right;
            transform.localEulerAngles = rightTurn;
        }
        
        
        // charge
        if (Input.GetKey(KeyCode.LeftControl) == true)
        {
            if (currentBulletForce < maxBulletForce)
            {
                currentBulletForce += bulletForceStep;
            }
        }

        // fire
        if (Input.GetKeyUp(KeyCode.LeftControl) == true)
        {
            GameObject newBullet = Instantiate(inBulletPrefab, inBulletSpawnPosition.position, Quaternion.identity) as GameObject;
            Rigidbody bulletRB = newBullet.GetComponent<Rigidbody>();
            Rigidbody playerRB = rigidbody;
            bulletRB.AddForce(currentAngle * currentBulletForce);

            // reset bullet force
            currentBulletForce = minBulletForce;
        }
    }
}
